

public class DecodageHuffman {
	
	private static final int R = 256;
    private String tab[]=new String[R];
    private char chartab[]=new char[R];
    
	
	public String DecodagesemiAdaptatif (String s){
		String decompressed="";
		//System.out.println(s);
		
		int i=0;
		while(tab[i]!=null && s.length()!=0 ){
			int a=tab[i].length();
			//System.out.println(s.subSequence(0, a)+"--"+tab[i]+" "+chartab[i]);
			String sub=""+ s.subSequence(0, a);
			if(sub.matches(tab[i])){
				decompressed+=chartab[i];
				s=s.substring(a);
				i=-1;
			}
			i++;
			}
System.out.println(decompressed);
		
		return decompressed;
	}
	
	public void TableMaker(String s){
		//System.out.println(s);
		int index=0;
		while (!s.matches("")){
			chartab[index]=s.charAt(0);
			s=s.substring(1);
			String code="";
			while (s.length()!=0 &&(s.charAt(0)=='0' || s.charAt(0)=='1')){
				code+=s.charAt(0);
				s=s.substring(1);
			}
			tab[index]=code;
			/*System.out.println("code="+code+"char="+chartab[index]);
			System.out.println("");
			System.out.println(s);*/
			index++;
		}
		
	}

}
